﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinFu.AOP.Interfaces;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.IoC;
using LinFu.Proxy.Interfaces;
using System.ComponentModel;

namespace CoreEx.Common.Tests
{
    [PostProcessor]
    public class SampleDomainModelPostProcessor : IPostProcessor
    {
        private IProxyFactory _proxyFactory;
        
        public void PostProcess(IServiceRequestResult result)
        {            
            if (result.ServiceType.Namespace.Contains("DomainModel"))
            {
                var proxyFactory = CreateProxyFactory(result.Container);
                var interceptor = result.Container.GetService<IInterceptor>("DataErrorInfoInterceptor",result.OriginalResult,result.ServiceType);
                var proxy = proxyFactory.CreateProxy(result.ServiceType, interceptor, typeof (IDataErrorInfo));
                result.ActualResult = proxy;
            }
        }

        private IProxyFactory CreateProxyFactory(IServiceContainer serviceContainer)
        {
            if (_proxyFactory == null)
                _proxyFactory = serviceContainer.GetService<IProxyFactory>();
            return _proxyFactory;
        }        
    }
}
